import { areaList } from "./data";
Component({
  data: {
    isFirstIn: true,
    value: [0, 0, 0],
    formerValue: [],
    provinceList: [],
    cityList: [],
    countyList: [],
  },
  didMount() {
    setTimeout(() => {
      this.setData({
        isFirstIn: false,
      });
    }, 1000);

    this.setData({
      value: this.props.initVal,
      provinceList: areaList,
      cityList: areaList[0].list,
      countyList: areaList[0].list[0].list,
    });
  },
  props: {
    isShowPicker: false,
    initVal: [],
  },
  methods: {
    onChange(e) {
      let totalList = areaList;

      let formerValue = this.data.value;

      let value = e.detail.value;

      let cityList = this.data.cityList;

      let countyList = this.data.countyList;

      if (formerValue[0] != value[0]) {
        cityList = totalList[value[0]].list;

        countyList = cityList[0].list;
        value = [value[0], 0, 0];
      } else {
        if (formerValue[1] != value[1]) {
          countyList = cityList[value[1]].list;
          value = [value[0], value[1], 0];
        }
      }

      this.setData({
        value: value,
        cityList,
        countyList,
      });
    },
    cancel() {
      this.props.cancel && this.props.cancel();
    },
    confirm() {
      let value = this.data.value;
      if (JSON.stringify(value) == "[]") {
        value = [0, 0, 0];
      }
      this.props.confirm &&
        this.props.confirm({
          province: this.data.provinceList[value[0]],
          city: this.data.cityList[value[1]],
          county: this.data.countyList[value[2]],
        });
    },
  },
});
